package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.MerchantTradeAmountRandomRuleDTO;
import com.ayf.payment.game.api.entity.MerchantTradeAmountRandomRule;
import com.ayf.payment.game.api.mapper.MerchantTradeAmountRandomRuleMapper;
import com.ayf.payment.game.api.service.MerchantTradeAmountRandomRuleService;
import com.ayf.payment.game.api.utils.BigDecimalUtil;
import com.ayf.payment.game.api.utils.DateUtils;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class MerchantTradeAmountRandomRuleServiceImpl implements MerchantTradeAmountRandomRuleService{

    @Resource
    private MerchantTradeAmountRandomRuleMapper merchantTradeAmountRandomRuleMapper;

    @Override
    public List<MerchantTradeAmountRandomRuleDTO> getMerchantTradeAmountRandomRuleList(MerchantTradeAmountRandomRuleDTO merchantTradeAmountRandomRuleDTO) {
        return merchantTradeAmountRandomRuleMapper.getMerchantTradeAmountRandomRuleList(merchantTradeAmountRandomRuleDTO);
    }

    @Override
    public void saveAndUpMerchantTradeAmtRule(MerchantTradeAmountRandomRuleDTO merchantTradeAmountRandomRuleDTO) {
        if (merchantTradeAmountRandomRuleDTO.getId() == null){
            Date currentDate = DateUtils.getNowDate();
            merchantTradeAmountRandomRuleDTO.setCreateTime(currentDate);
            merchantTradeAmountRandomRuleDTO.setLastUpdateTime(currentDate);
            merchantTradeAmountRandomRuleMapper.insert(BeanConvertUtils.convert(merchantTradeAmountRandomRuleDTO, MerchantTradeAmountRandomRule.class));
        } else {
            Date currentDate = DateUtils.getNowDate();
            merchantTradeAmountRandomRuleDTO.setLastUpdateTime(currentDate);
            merchantTradeAmountRandomRuleMapper.updateByPrimaryKeySelective(BeanConvertUtils.convert(merchantTradeAmountRandomRuleDTO,MerchantTradeAmountRandomRule.class));
        }
    }

    @Override
    public void deleteMerchantTradeAmtRuleById(Integer id) {
        MerchantTradeAmountRandomRule merchantTradeAmountRandomRule = new MerchantTradeAmountRandomRule();
        merchantTradeAmountRandomRule.setId(id);
        merchantTradeAmountRandomRuleMapper.delete(merchantTradeAmountRandomRule);
    }

    @Override
    public BigDecimal getMerchantTradeAmountRandomRule(Integer merchantId, BigDecimal checkAmt) {
        BigDecimal randomAmt = BigDecimal.valueOf(0.00);
        MerchantTradeAmountRandomRule merchantTradeAmountRandomRule = merchantTradeAmountRandomRuleMapper.getMerchantTradeAmountRandomRule(merchantId,checkAmt);
        if (Objects.isNull(merchantTradeAmountRandomRule)){
            return randomAmt;
        }
        try {
            randomAmt = BigDecimalUtil.randomAmt(merchantTradeAmountRandomRule.getRandomMinAmt(),merchantTradeAmountRandomRule.getRandomMaxAmt());
            LogPortal.info("商户ID:{} 充值金额:{} 获取随机数金额:{}",merchantId,checkAmt,randomAmt);
        } catch (Exception e) {
            LogPortal.error("商户ID:{} 充值金额:{} 获取随机数金额发生{}异常",e,merchantId,checkAmt,e.getClass().getName());
        }
        return randomAmt;
    }

    @Override
    public boolean existMerchantTradeRule(Integer merchantId) {
        MerchantTradeAmountRandomRule merchantTradeAmountRandomRule = new MerchantTradeAmountRandomRule();
        merchantTradeAmountRandomRule.setMerchantId(merchantId);
        int count = merchantTradeAmountRandomRuleMapper.selectCount(merchantTradeAmountRandomRule);
        return count>0?true:false;
    }
}
